package domo1;

import domo1.utils.DButils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.text.MessageFormat;
import java.util.Scanner;

public class insert {
    public static void main(String[] args) {
        Connection connection=null;
        PreparedStatement statement=null;
        //增删改不返回结果集
        try{
            connection=DButils.geconnecton();
            //定义SQL语句
            String sql="insert into exam (name,chinese,math,english) values(?,?,?,?)";
            statement=connection.prepareStatement(sql);
            //输入插入数据信息
            System.out.println("输入名称");
            Scanner scanner=new Scanner(System.in);
            String name=scanner.next();
            System.out.println("输入语文成绩");
            statement.setString(1,name);
            int chinese= scanner.nextInt();
            System.out.println("输入数学成绩");
            statement.setInt(2,chinese);
            int math= scanner.nextInt();
            System.out.println("输入英语成绩" );
            statement.setInt(3,math);
            int english=scanner.nextInt();
            statement.setInt(4,english);
            int row=statement.executeUpdate();
            if (row==1){
                System.out.println("插入成功");
            }else {
                System.out.println("插入失败");
            }
            System.out.println(MessageFormat.format("name={0},chinese={1},math={2},english={3}", name, chinese, math, english));
        } catch (SQLException e) {
            e.printStackTrace();
        }
        finally {
            DButils.close(null,statement,connection);
        }
    }
}
